package com.android;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sql.DAO;

import com.Model.Pojo.Users;

import com.Model.DengLuModel.PanDuanZhangHao;

public class AndroidPost extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
    	response.setCharacterEncoding("UTF-8");
    	PrintWriter out = response.getWriter();
    	
    	String name = request.getParameter("username");
		String pass = request.getParameter("password");
		String shenfen = request.getParameter("shenfen");
		PanDuanZhangHao panduan = new PanDuanZhangHao(name, pass, shenfen); //实例化判断账号密码的对象
		String flag = panduan.panDuan(); //判断账号密码正确并获取返回值
		String sql = panduan.pingsql(flag);//拼接sql语句
		DAO dao = new DAO();
		ArrayList<Users> list = new ArrayList<Users>();
		Users users = new Users();
		list = dao.chaxun(sql); //查询账号密码对应的详细信息
		for(Users user : list){ //将信息存在user里面
			users = (Users) user;
		}
		String username = "username";
		String password = "password";
		String yue = "yue";
		String xingming = "xingming";
		String shoujihao = "shoujihao";
		String youxiang = "youxiang";
		String money = "money";
		String type = request.getParameter("type");//获取请求的类型，判断并返回相应的值
		String option = request.getParameter("option");//获取请求的操作，判断应该执行哪个
		String result = null;
		if(option.equals("get")){
			if(type.equals(username)){
				result = users.getZhanghao();
			}else if(type.equals(password)){
				result = users.getMima();
			}else if(type.equals(yue)){
				result = users.getYue() + "";
			}else if(type.equals(xingming)){
				result = users.getXingming();
			}else if(type.equals(shoujihao)){
				result = users.getShoujihao();
			}else if(type.equals(youxiang)){
				result = users.getYouxiang();
			}else if(type.equals(money)){
				result = users.getMoney() + "";
			}else{
				result = "false";
			}
		}else if(option.equals("set")){
			String value = request.getParameter("value");//获取请求的参数，做出相应修改
			result = "success";
			if(type.equals(password)){
				users.xiugaimima(value);
			}else if(type.equals(yue)){
				int val = Integer.parseInt(value);
				users.xiugaiyue(val);
			}else if(type.equals(xingming)){
				users.xiugaixingming(value);
			}else if(type.equals(shoujihao)){
				if(users.isShoujihao(value)){
					result = "fail";
				}else{
					users.xiugaishoujihao(value);
				}
			}else if(type.equals(youxiang)){
				if(users.isYouxiang(value)){
					result = "fail";
				}else{
					users.xiugaiyouxiang(value);
				}
			}else if(type.equals(money)){
				int val = Integer.parseInt(value);
				users.xiugaimoney(val);
			}else{
				result = "false";
			}
		}else{
			result = "false";
		}
		out.print(result);
		
		out.flush();
		out.close();
	}
}
